package shenxg;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;

class ColorType {
	public int amount = 0;
	public Color color = null;
}

public class GameMain {

	static int x = 44;
	static int y = 223;
	static int width = 68;

	public static ArrayList<ColorType> colorTypeList = new ArrayList<ColorType>();

	public static void main(String[] args) {
		getColorList(testImage());
		colorPrint();
	}
	
	public static void colorPrint()
	{
		BufferedImage image = null;
		ArrayList<Color> list = new ArrayList<Color>();
		try {
			File file = new File("e:/temp/1.jpg");
			File imgOutput = new File("e:/temp/1_out_color_print.jpg");

			image = ImageIO.read(file);
			Graphics g = image.getGraphics();
			
			int k = 0;
			for (ColorType colorType : colorTypeList) {
				k++;

				g.setColor(colorType.color);
				g.fillRect(x, y+k*width, width*10, width);
			}

			ImageIO.write(image, "jpg", imgOutput);


		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void getColorList(ArrayList<Color> colors) {
		for (Color color : colors) {
			ColorType sameColorType = null;
			for (ColorType colorType : colorTypeList) {
				int diffR = 0;
				int diffG = 0;
				int diffB = 0;
				int diffAll = 0;

				diffR = colorType.color.getRed() - color.getRed();
				diffG = colorType.color.getGreen() - color.getGreen();
				diffB = colorType.color.getBlue() - color.getBlue();

				diffAll = Math.abs(diffR) + Math.abs(diffG) + Math.abs(diffB);
				if (diffAll < 50) {
					sameColorType = colorType;
					break;
				}
			}

			if (null == sameColorType) {
				sameColorType = new ColorType();
				sameColorType.color = new Color(color.getRed(),
						color.getGreen(), color.getBlue());
				colorTypeList.add(sameColorType);
			}

			sameColorType.amount++;
		}

		for (ColorType colorType : colorTypeList) {
			System.out.println("ColorType R:" + colorType.color.getRed()
					+ " G:" + colorType.color.getGreen() + " B:"
					+ colorType.color.getBlue() + "  amount:"
					+ colorType.amount);
		}
	}

	private static ArrayList<Color> testImage() {
		BufferedImage image = null;
		ArrayList<Color> list = new ArrayList<Color>();
		try {
			File file = new File("e:/temp/1.jpg");
			File imgOutput = new File("e:/temp/1_out.jpg");

			image = ImageIO.read(file);
			Graphics g = image.getGraphics();

			for (int j = 0; j < 10; j++) {
				for (int i = 0; i < 10; i++) {
					Color color = new Color(image.getRGB(x + width * i + width
							/ 2, y + width * j + width / 2));
					list.add(color);
					g.setColor(color);
					g.fillRect(x + width * i, y + width * j, width, width);

					g.fillRect(x + width * i, y + width * j, width, width);
				}
			}

			g.drawRect(x, y, width, width);

			ImageIO.write(image, "jpg", imgOutput);

			return list;

		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

}
